package com.hsr.web.config;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;

/**
 * @author tb
 * @create 2022-04-03 22:56
 * @remark
 */
@Component
public class CustomizeFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    // ant路径匹配的类
    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
        //获取请求地址
        String requestUrl = ((FilterInvocation) o).getRequestUrl().split("\\?")[0];
        for(String url: StaticSource.source){
            if(antPathMatcher.match(url,requestUrl)){
                return null;
            }
        }
        String[] attributes = new String[]{"admin"};
        return SecurityConfig.createList(attributes);
//        //包含list接口不需要鉴权
//        if (requestUrl.contains("list")){
//            return null;
//        }else{
//            String[] attributes = new String[]{"admin"};
//            return SecurityConfig.createList(attributes);
//        }
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
